package mensagens;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import o2b.IStream;
import o2b.MarshallableException;
import cliente.tipos.Reserva;

public class ProcurarReserva extends Mensagem {

	protected Reserva reserva;
	
	public ProcurarReserva() {
		reserva = new Reserva();
		reserva.setDataInicio(null);
		reserva.setDataFim(null);
		reserva.getHospedeId().setId(0);
		reserva.setValorEntrada(0.0);
		reserva.setTipo(0);
	}
	
	@Override
	public void marshal(IStream stream) throws MarshallableException {
		super.marshal(stream);
		stream.write(getReserva().getHospedeId().getId());
		stream.write(reserva.getDataInicio().toString());
		stream.write(reserva.getDataFim().toString());
		stream.write(reserva.getValorEntrada());
	}
	
	@Override
	public void unmarshal(IStream stream) throws MarshallableException {
		super.unmarshal(stream);
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		getReserva().getHospedeId().setId(stream.readInt());
		try {
			getReserva().setDataInicio(df.parse((String) stream.readObject()));
			getReserva().setDataFim(df.parse((String) stream.readObject()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		getReserva().setValorEntrada(stream.readDouble());
	}

	public Reserva getReserva() {
		return reserva;
	}

	public void setReserva(Reserva reserva) {
		this.reserva = reserva;
	}
}
